package nyist.media.controller;

import nyist.media.bean.auth.MenuTreeBean;
import nyist.media.bean.role.RoleInfoBean;
import nyist.media.entity.admin.Admin;
import nyist.media.page.ApiResult;
import nyist.media.service.AdminService;
import nyist.media.service.MenuAuthService;
import nyist.media.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 选项列表
 *
 * @author dcf
 */
@RestController
@RequestMapping(value = "/admin/options", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OptionRestController extends BaseController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuAuthService menuAuthService;
    @Autowired
    private AdminService adminService;
    /**
     * 查询角色下拉列表
     *
     * @return 下拉列表
     */
    @GetMapping(params = "role")
    public Map<String, Object> roles() {
        return ApiResult.SUCCESS.getMap().add("result", roleService.findAll());
    }

    /**
     * 查询权限下拉列表
     *
     * @return 下拉列表信息
     */
    @GetMapping(params = "menu")
    public Map<String, Object> menus() {
        return ApiResult.SUCCESS.getMap().add("result", menuAuthService.findOptions());
    }

    /**
     * 查询权限树
     *
     * @param roleId 角色ID
     * @return 权限树信息
     */
    @GetMapping(params = "menu_tree")
    public List<MenuTreeBean> menuTree(Long roleId) {
        List<MenuTreeBean> tree = menuAuthService.findRoot();
        if (roleId == null) {
            return tree;
        }
        RoleInfoBean info = roleService.getInfo(roleId);
        MenuTreeBean.findChecked(info.getMenuAuthIds(), tree);
        return tree;
    }

    /**
     * 查询维修员列表
     * @return 维修员列表
     */
    @GetMapping(params = "repairmen")
    public Map<String, Object> repairmen() {
        return ApiResult.SUCCESS.getMap().add("result", adminService.findList());
    }

}
